package com.ft.oa.service.dev.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.dev.DevProblem;
import com.ft.oa.domain.dev.DevProblemItem;
import com.ft.oa.domain.dev.dto.DevProblemDto;
import com.ft.oa.domain.development.DevAllocate;
import com.ft.oa.domain.dto.ApiResult;
import com.ft.oa.domain.sys.SysUser;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.dev.DevProblemItemMapper;
import com.ft.oa.mapper.dev.DevProblemMapper;
import com.ft.oa.mapper.development.DevAllocateMapper;
import com.ft.oa.mapper.sys.SysUserMapper;
import com.ft.oa.service.dev.DevProblemService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: yihui
 * Date: 2022/1/18 10:15 AM
 * FileName: DevProblemServiceImpl
 * Description: 研发问题
 */
@Transactional
@Service
public class DevProblemServiceImpl implements DevProblemService {
    
    @Resource
    DevProblemMapper devProblemMapper;

    @Resource
    DevProblemItemMapper devProblemItemMapper;

    @Resource
    DevAllocateMapper devAllocateMapper;

    @Resource
    SysUserMapper sysUserMapper;

    @Override
    public ApiResult add(DevProblem devProblem) {
        Long devAllocateId = devProblem.getDevAllocateId();
        DevAllocate devAllocate = devAllocateMapper.selectById(devAllocateId);
        //审核通过的项目才能创建任务 审核状态 0-审核中  1-通过  2-驳回
        if(  1 != devAllocate.getApprovalStatus()){
            return ApiResult.builder().code(30001).msg("审批未通过，无法创建研发问题").build();
        }
        devProblem.setDevAllocateName(devAllocate.getDevAllocateName());
        devProblem.setProblemStatus(0);
        Long userId = devProblem.getPrincipalStaffId();
        SysUser user = sysUserMapper.selectById(userId);
        devProblem.setPrincipalStaffName(user.getUsername());
        devProblem.setCreateDate(LocalDate.now());
        devProblem.setCreateDateTime(LocalDateTime.now());
        devProblem.setProgressRate(0);
        Long createStaffId = SecurityUtils.getCurrentUser().getId();
        devProblem.setCreateStaffId(createStaffId);
        devProblemMapper.insert(devProblem);
        return ApiResult.simpleSuccess();
    }

    @Override
    public ApiResult del(DevProblem devProblem) {
        LocalDateTime nowDate = LocalDateTime.now();
        LambdaUpdateChainWrapper<DevProblemItem> wrapperItem = new LambdaUpdateChainWrapper<>(devProblemItemMapper);
        boolean updateItem = wrapperItem.eq(DevProblemItem::getDevProblemId, devProblem.getId())
                .set(DevProblemItem::getDel, true)
                .set(DevProblemItem::getDelTime, nowDate)
                .update();

        LambdaUpdateChainWrapper<DevProblem> wrapper = new LambdaUpdateChainWrapper<>(devProblemMapper);
        boolean update = wrapper.eq(DevProblem::getId, devProblem.getId())
                .set(DevProblem::getDel, true)
                .set(DevProblem::getDelTime, nowDate)
                .update();
        return ApiResult.simpleSuccess();
    }

    @Override
    public Page<DevProblem> page(DevProblem condition) {
        Page<DevProblem> pages = new Page<>(condition.getPage(), condition.getSize());
        QueryWrapper<DevProblem> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(DevProblem::getDel,false)
                .eq(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager()), DevProblem::getCreateStaffId,SecurityUtils.getCurrentUser().getId())
                .ge(StringUtils.hasText(condition.getCreateStartDate()),DevProblem::getCreateDate,condition.getCreateStartDate())
                .le(StringUtils.hasText(condition.getCreateEndDate()),DevProblem::getCreateDate,condition.getCreateEndDate())
                .ge(StringUtils.hasText(condition.getPlanFinishStartDate()),DevProblem::getPlanFinishDate,condition.getPlanFinishStartDate())
                .le(StringUtils.hasText(condition.getPlanFinishEndDate()),DevProblem::getPlanFinishDate,condition.getPlanFinishEndDate())
                .ge(StringUtils.hasText(condition.getActualFinishStartDate()),DevProblem::getActualFinishDate,condition.getActualFinishStartDate())
                .le(StringUtils.hasText(condition.getActualFinishEndDate()),DevProblem::getActualFinishDate,condition.getActualFinishEndDate())
                .eq(null != condition.getDevAllocateId() ,DevProblem::getDevAllocateId, condition.getDevAllocateId())
                .like(StringUtils.hasText(condition.getPrincipalStaffName()) ,DevProblem::getPrincipalStaffName, condition.getPrincipalStaffName());
        wrapper.select().orderByDesc("create_date_time");
        Page<DevProblem> result = devProblemMapper.selectPage(pages, wrapper);
        return result;
    }

    @Override
    public ApiResult view(DevProblem devProblem) {
        DevProblemDto devProblemDto = new DevProblemDto();
        DevProblem devProblem1 = devProblemMapper.selectById(devProblem.getId());
        devProblemDto.setDevProblem(devProblem1);
        Map queryMap  = new HashMap();
        queryMap.put("dev_problem_id",devProblem.getId());
        List<DevProblemItem> devProblemItemList = devProblemItemMapper.selectByMap(queryMap);
        devProblemDto.setDevProblemItemList(devProblemItemList);
        return ApiResult.simpleSuccess().setData(devProblemDto);
    }




}
